package com.system.product.controller;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.system.product.domain.R;
import com.system.product.request.CartItemRequest;
import com.system.product.service.CartService;
import com.system.product.util.JsonData;
import com.system.product.vo.CartItemVO;
import com.system.product.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/cart")
public class CartController {


    @Autowired
    private CartService cartService;

    @RequestMapping("/add")
    public R addCart(@RequestBody  CartItemRequest request){
        System.out.println("request :"+ request);
        cartService.addToCart(request);
        return R.ok();
    }

    /**
     * 用于订单服务，确认订单，获取对应的商品项详情信息
     *
     * 会清空购物车的商品数据
     * @param productIdList
     * @return
     */
    @PostMapping("confirm_order_cart_items")
    public JsonData confirmOrderCartItems(@RequestBody List<Long> productIdList){

        List<CartItemVO> cartItemVOList = cartService.confirmOrderCartItems(productIdList);

        return JsonData.buildSuccess(cartItemVOList);

    }
    @GetMapping("/mycart")
    public R findMyCart(){

        CartVO cartVO = cartService.getMyCart();
        HashMap resultMap = new HashMap();
        resultMap.put("cartItems",cartVO.getCartItems());
        resultMap.put("totalNum",cartVO.getTotalNum());
        resultMap.put("totalAmount",cartVO.getTotalAmount());
        resultMap.put("realPayAmount",cartVO.getRealPayAmount());

        return R.ok(resultMap);
    }
}
